package jsu.blogger.service.impl;

import jsu.blogger.pojo.WebSocketMessage;
import jsu.blogger.util.MessageUtil;
import jsu.blogger.websocket.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class SystemNoticeService {
    private static final Logger log = LoggerFactory.getLogger(SystemNoticeService.class);
    private static ApplicationContext applicationContext;
    private static WebSocketServer webSocketServer;
    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
        webSocketServer = applicationContext.getBean(WebSocketServer.class);
    }


    public  static void  noticeFansFollow(Long senderId, Long receiverId,String sender,String way, Timestamp time){
        //首先查看推送用户是否在线
        if(!isNotice(senderId,receiverId))return;
        String msg="粉丝关注通知";
//        log.info(msg);
        Map<String,Object> map=new HashMap<>();
        map.put("sender",sender);
        map.put("senderId",senderId);
        map.put("receiverId",receiverId);
        map.put("way",way);
        map.put("time",time);
        String  message=MessageUtil.toJsonWebSocketMessage(4,msg,map);
        try{
//            webSocketServer.getOnlineUserList().forEach((key, value) -> {
//                System.out.println("用户：" + key + " 在线");
//            });
            if(webSocketServer.getOnlineUserList().containsKey(receiverId.toString())){
                webSocketServer.getOnlineUserList().get(receiverId.toString()).getBasicRemote().sendText(message);
            }
            //不在线就将消息存储起来
            else{
                ArrayList<String> tempMessages =webSocketServer.getTempMessageList().computeIfAbsent(String.valueOf(receiverId), k -> new ArrayList<>());
                tempMessages.add(message);
                log.info("消息暂存："+message);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }
    public  static void  noticeLiveLike(Long senderId, Long receiverId, Long postId, String sender, String title, Timestamp time){
        //首先查看推送用户是否在线
        if(!isNotice(senderId,receiverId))return;
        String msg="点赞了你的帖子";
        Map<String,Object> map=new HashMap<>();
        map.put("sender",sender);
        map.put("senderId",senderId);
        map.put("receiverId",receiverId);
        map.put("title",title);
        map.put("postId",postId);
        map.put("time",time);
        String  message=MessageUtil.toJsonWebSocketMessage(5,msg,map);
        try{
//            webSocketServer.getOnlineUserList().forEach((key, value) -> {
//                System.out.println("用户：" + key + " 在线");
//            });
            if(webSocketServer.getOnlineUserList().containsKey(receiverId.toString())){
                webSocketServer.getOnlineUserList().get(receiverId.toString()).getBasicRemote().sendText(message);
            }
            //不在线就将消息存储起来
            else{
                ArrayList<String> tempMessages =webSocketServer.getTempMessageList().computeIfAbsent(String.valueOf(receiverId), k -> new ArrayList<>());
                tempMessages.add(message);
                log.info("消息暂存："+message);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }
    public  static void  noticePostComment(Long senderId, Long receiverId, Long postId,String comment, String sender, String title, Timestamp time){
        //首先查看推送用户是否在线
        if(!isNotice(senderId,receiverId))return;
        String msg="评论了你的帖子";
        Map<String,Object> map=new HashMap<>();
        map.put("sender",sender);
        map.put("senderId",senderId);
        map.put("receiverId",receiverId);
        map.put("title",title);
        map.put("postId",postId);
        map.put("time",time);
        map.put("comment",comment);
        String  message=MessageUtil.toJsonWebSocketMessage(6,msg,map);
        try{
//            webSocketServer.getOnlineUserList().forEach((key, value) -> {
//                System.out.println("用户：" + key + " 在线");
//            });
            if(webSocketServer.getOnlineUserList().containsKey(receiverId.toString())){
                webSocketServer.getOnlineUserList().get(receiverId.toString()).getBasicRemote().sendText(message);
            }
            //不在线就将消息存储起来
            else{
                ArrayList<String> tempMessages =webSocketServer.getTempMessageList().computeIfAbsent(String.valueOf(receiverId), k -> new ArrayList<>());
                tempMessages.add(message);
                log.info("消息暂存："+message);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }
    public  static void  noticeReply(Long senderId, Long receiverId, Long postId,String sender,String myComment , String otherComment, Timestamp time){
        //首先查看推送用户是否在线

        System.out.println("senderId:"+senderId+"receiverId:"+receiverId+"postId:"+postId);

        if(!isNotice(senderId,receiverId))return;
        String msg="回复了你的评论";
        Map<String,Object> map=new HashMap<>();
        map.put("sender",sender);
        map.put("senderId",senderId);
        map.put("myComment",myComment);
        map.put("receiverId",receiverId);
        map.put("postId",postId);
        map.put("time",time);
        map.put("otherComment",otherComment);
        String  message=MessageUtil.toJsonWebSocketMessage(7,msg,map);
        try{
//            webSocketServer.getOnlineUserList().forEach((key, value) -> {
//                System.out.println("用户：" + key + " 在线");
//            });
            if(webSocketServer.getOnlineUserList().containsKey(receiverId.toString())){
                webSocketServer.getOnlineUserList().get(receiverId.toString()).getBasicRemote().sendText(message);
            }
            //不在线就将消息存储起来
            else{
                ArrayList<String> tempMessages =webSocketServer.getTempMessageList().computeIfAbsent(String.valueOf(receiverId), k -> new ArrayList<>());
                tempMessages.add(message);
                log.info("消息暂存："+message);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }
    public static boolean isNotice(Long senderId, Long receiverId){
        return  senderId==receiverId?false:true;
    }
}
